package com.guigu.tankai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.liuyunan.mapper.SetmealMapper;
import com.guigu.liuyunan.pojo.Setmeal;
import com.guigu.tankai.mapper.MemberMapper;
import com.guigu.tankai.mapper.MemberinfoMapper;
import com.guigu.tankai.mapper.UserMapper;
import com.guigu.tankai.pojo.Member;
import com.guigu.tankai.pojo.Memberinfo;
import com.guigu.tankai.pojo.PageVo;
import com.guigu.tankai.pojo.User;
import com.guigu.tankai.service.MemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemServiceImpl implements MemService {
    @Autowired
    MemberMapper me;
    @Autowired
    MemberinfoMapper memberinfoMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public PageVo<Member> findByMembenation(Integer page, Integer num, Member member) {
        PageHelper.startPage(page,num);
        List<Member> byMemberation = me.findByMemberation(member);

        PageInfo pageInfo = new PageInfo(byMemberation);
        PageVo<Member> PageVo = new PageVo<Member>();
        PageVo.setTotal(pageInfo.getTotal());
        PageVo.setRows(pageInfo.getList());
        return PageVo;
    }

    @Override
    public PageInfo<Member> findByMembertion(Integer page, Integer num, String member) {
        PageHelper.startPage(page,num);
        List<Member> byMembertion = me.findByMembertion(member);

        for (Member member1 : byMembertion) {
            Memberinfo memberinfo = memberinfoMapper.selectByPrimaryKey(member1.getId());
            member1.setMemberinfo(memberinfo);
            if (member1.getHiduser()!=null){
                User user = userMapper.selectByPrimaryKey(member1.getHiduser());
                if (user!=null){
                    member1.setUser(user);
                }
            }
        }
        return new PageInfo<>(byMembertion);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return me.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(Member record) {
        int i = me.insert(record);
        return record.getId();
    }

    @Override
    public Member selectByPrimaryKey(Integer id) {
        return me.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Member record) {
        return me.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Member record) {
        return me.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Setmeal> selectByP(Integer id) {
        return setmealMapper.selectByP(id);
    }

    @Override
    public List<Member> tklist() {
        return me.tklist();
    }
}
